//
// Created by zhengjuping on 24-9-9.
//

#ifndef ENV_II_BT_GATTCHARACTERISTIC_H
#define ENV_II_BT_GATTCHARACTERISTIC_H

#include <esp_gatt_defs.h>
#include "BleUuid.h"
#include "string"
#include "GattAttributeTable.h"
#include "memory"

class GattCharacteristic {

public:
    enum class ResponseMethod{
        RSP_BY_APP,
        AUTO_RSP
    };

    enum class Properties{
        Read,
        Write,
        Notify,
        Read_Write_Notify,
    };
    enum class Permissions{
        Read,
        Write,
        Read_Write,
    };
    explicit GattCharacteristic( BleUuid const &uuid,
                        const uint8_t *defaultValue,
                        uint32_t defaultValueSize,
                        ResponseMethod rspMethod,
                        Properties properties,
                        Permissions permission );

    virtual ~GattCharacteristic() = default;

    const esp_gatts_attr_db_t *getAttrDb();
    void dump();

    void setHandle( uint16_t handle );
    uint16_t getHandle() const;

    uint32_t getProperties();

    void setGattsIf( uint16_t gattsIf );
    void setConnId( uint16_t connId );

    int32_t sendIndicate( uint8_t *data, uint16_t length ) const;

    virtual void onConnectEvent();
    virtual void onDisConnectEvent();

    virtual void onReadEvent( uint8_t *data, uint16_t &length );
    virtual void onWriteEvent(const uint8_t *data, uint16_t length );
    virtual void onNotifyStatusUpdateEvent( bool notifyEnabled );
private:
    BleUuid _uuid;
    uint16_t _uuid16;
    uint32_t _uuid32;
    esp_gatts_attr_db_t _attr_db;

    uint16_t _gattsIf;
    uint16_t _connId;
    uint16_t _handle;

    uint16_t _rspMethod;
    uint16_t _properties;
    uint16_t _permission;

    uint8_t *_defaultValue = nullptr;
    uint32_t _defaultValueSize  = -1;

    uint32_t _max_length;
};


#endif //ENV_II_BT_GATTCHARACTERISTIC_H
